package model;

import java.util.ArrayList;
import java.util.Map;
import java.util.Random;

public class Ind_Fourmi extends Individu{
	
	private static Random randomSeed = new java.util.Random();
	private static int idGenerator=0;
	private enum State{
		oeuf , larve , nymphe , adulte;
	}
	
	private Role role;
	private State etat;	
	private Fourmiliere fourmiliere;
	private int id;
	private ArrayList<Action> ActionList = new ArrayList<Action>();
	private Action doingAction;
	private int hungryCounter;
	private int evolveCounter;
	private int fatigueCounter;
	
	
	public Ind_Fourmi(int _jourNaissance,int _X,int _Y,Fourmiliere _fourmiliere,Role _role){
		jourNaissance = _jourNaissance;
		posX = _X;
		posY = _Y;
		etat = State.oeuf;
		fourmiliere = _fourmiliere;
		lifeTime = 550+(randomSeed.nextInt(350));
		poid = 15+(randomSeed.nextInt(10));
		id = idGenerator;
		idGenerator++;
		ActionList.add(new Act_Fou_Manger(this));
		ActionList.add(new Act_Fou_Reposer(this));
		doingAction = ActionList.get(1);
		setRole(_role);
		evolveCounter = MonTimer.u_jour *3;
		hungryCounter = MonTimer.u_jour;
		fatigueCounter = MonTimer.u_jour/2;
		cellPos = Territoire.getCells(_X, _Y);
	}
	
	
	public String getEtat(){
		return etat.toString();
	}
	
	public Role getRole(){
		return role;
	}
	
	public int getId(){
		return id;
	}
	
	public void setRole(Role _role){
		role = _role;
		role.linkToHote(this);
	}
	
	public void initGrows(){
		etat = State.adulte;
	}
	
	public void add_Action(Action _action){
		ActionList.add(_action);
	}
	
	public boolean atHome (){
		if(posX == fourmiliere.getX() && posY == fourmiliere.getY()) return true;
		return false;
	}
	
	public void setHungryCounter(int _count){
		hungryCounter = _count;
	}
	
	public void setFatigueCounter(int _count){
		fatigueCounter = _count;
	}
	
	public int getFatigueCounter(){
		return fatigueCounter;
	}
	
	public int getHungryCount(){
		return hungryCounter;
	}
	private boolean IsAlive(){
		if(etat == State.larve || etat == State.adulte){
			if(hungryCounter<=0){
				return false;
			}
		}
		if(lifeTime<=0){
			return false;
		}
		if(fatigueCounter<=0){
			return false;
		}
		return true;
		
	}
	
	public void setFourmiliere(Fourmiliere _fourmiliere){
		fourmiliere = _fourmiliere;
	}
	public Fourmiliere getFourmiliere(){
		return fourmiliere;
	}
	
	
	public void buildReport (Map<String,Object> r){
		if(r.get(etat.toString()) == null){
			r.put(etat.toString(), 1);
		}else{
			r.put(etat.toString(), (Integer)(r.get(etat.toString()))+1);
		}
		
		if(role != null && etat.toString() == "adulte"){
			if(role instanceof Rol_Guerriere)
				if(r.get("guerriere") == null)
					r.put("guerriere", 1);
				else
					r.put("guerriere", (Integer)(r.get("guerriere"))+1);
			else if(role instanceof Rol_Ouvriere)
				if(r.get("ouvriere") == null)
					r.put("ouvriere", 1);
				else
					r.put("ouvriere", (Integer)(r.get("ouvriere"))+1);
			else if(role instanceof Rol_Reine)
				if(r.get("reine") == null)
					r.put("reine", 1);
				else
					r.put("reine", (Integer)(r.get("reine"))+1);
			else if(role instanceof Rol_Sexue)
				if(r.get("sexue") == null)
					r.put("sexue", 1);
				else
					r.put("sexue", (Integer)(r.get("sexue"))+1);
		}else
			if(r.get("sans_role") == null)
				r.put("sans_role", 1);
			else
				r.put("sans_role", (Integer)(r.get("sans_role"))+1);
		
		
	}
	
	private void doAction(){

		doingAction.doAction();	
		if(etat == State.adulte){
			for(Action a:ActionList){		
				a.updatePriority();			
				if(a.getPriority()>doingAction.getPriority()){
					doingAction = a;
				}
			}
		}else if(etat == State.larve){
			ActionList.get(0).updatePriority();
			ActionList.get(1).updatePriority();
			
			if(ActionList.get(0).getPriority()>ActionList.get(1).getPriority()){
				doingAction = ActionList.get(0);
			}else{
				doingAction = ActionList.get(1);
			}
			
				
		}else{
			doingAction = ActionList.get(1);
		}
	}
	
	private void evolve(){
		if(etat == State.oeuf){
			etat = State.larve;
			poid *=4;
			evolveCounter = MonTimer.u_jour * 10;
		}else if(etat == State.larve){
			etat = State.nymphe;
			poid /=4;
			evolveCounter = MonTimer.u_jour * 17;
		}else if(etat == State.nymphe){
			etat = State.adulte;
			
		}
	}
	
	
	
	@Override
	public void update() {	
		if(IsAlive()){
			doAction();
			if(etat == State.adulte || etat == State.larve){
				hungryCounter-=MonTimer.getTimeUnit();
			}
			
			if(!atHome()){
				fatigueCounter-=MonTimer.getTimeUnit();
			}
			
			if(etat != State.adulte){
				evolveCounter-=MonTimer.getTimeUnit();
				if(evolveCounter<0){
					evolve();
				}
			}
			
		}else{			
			fourmiliere.killFourmi(this);
		}
	}
}
